//
//  HMGameView.m
//  SNS_Login-register
//

#import "HWLoginView.h"
#import "WHToast.h"
#import "HWGameMgr.h"
#import "HWConstant.h"
#import "HWKeychain.h"



@interface HWLoginView ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userAccountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwdTextfield;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *tips;
@property (weak, nonatomic) IBOutlet UIButton *switchBtn;
@property (weak, nonatomic) IBOutlet UIButton *regLoginBtn;
@property (weak, nonatomic) IBOutlet UIButton *checkBtn;
@property (nonatomic, assign) Boolean isAgree;
@property (nonatomic, assign) Boolean isLogin;


- (IBAction)registerLoginClick:(id)sender;
- (IBAction)switchLoginClick:(id)sender;
- (IBAction)userAgreementClick:(id)sender;
- (IBAction)privacyAgreementClick:(id)sender;
- (IBAction)checkBtnClick:(id)sender;


@end

@implementation HWLoginView

+ (instancetype)HWLoginView{
    NSString  *clsName = NSStringFromClass([self class]);
    UINib * nib = [UINib nibWithNibName:clsName bundle:nil];
    HWLoginView * obj = [nib instantiateWithOwner:nil options:nil].firstObject;
    [obj initGameView];
    return obj;
    
}

-(void)layoutSubviews{
}

-(void)initGameView{
    // 创建一个富文本对象
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 设置富文本对象的颜色
    attributes[NSForegroundColorAttributeName] = [UIColor grayColor];
    _userAccountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:attributes];
    _passwdTextfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:attributes];
    
    //输入框处理
    [_userAccountTextField addTarget:self action:@selector(LimitTextFieldLength:) forControlEvents:UIControlEventEditingChanged];
    [_passwdTextfield addTarget:self action:@selector(LimitTextFieldLength1:) forControlEvents:UIControlEventEditingChanged];
    
    _userAccountTextField.delegate = self;
    _passwdTextfield.delegate = self;
}


#pragma mark - 用户协议
- (IBAction)userAgreementClick:(id)sender {
    
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.passwdTextfield resignFirstResponder];
    [[HWGameMgr shared] showUserAgreement:kUserAgreementUrl];
}
#pragma mark - 隐私协议
- (IBAction)privacyAgreementClick:(id)sender {
    
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.passwdTextfield resignFirstResponder];
    [[HWGameMgr shared] showUserAgreement:kPrivateUrl];
}

#pragma mark - 注册
- (IBAction)registerLoginClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.passwdTextfield resignFirstResponder];
    if (_isAgree) {
        if (_isLogin) {
            [self login];
        } else {
            [self registe];
        }
        
    } else {
        [[HWGameMgr shared] showToastWithMsg:@"请先勾选《用户协议》、《隐私协议》"];
    }
}
#pragma mark - 切换登录
- (IBAction)switchLoginClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.passwdTextfield resignFirstResponder];
    if (_isLogin) {
        [self switchToReg:@{@"username":self.userAccountTextField.text,@"password":@""}];
    } else {
        [self switchToLogin:@{@"username":self.userAccountTextField.text,@"password":@""}];
    }
}
#pragma mark - check
- (IBAction)checkBtnClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.passwdTextfield resignFirstResponder];
    _isAgree = !_isAgree;
    NSString * title = _isAgree ? @"✓" : @"";
    UIColor * color = _isAgree ? [UIColor systemBlueColor] : [UIColor whiteColor];
    [_checkBtn setTitle:title forState:UIControlStateNormal];
    [_checkBtn setBackgroundColor:color];
}

- (void)switchToLogin:(NSDictionary*)dic
{
    _title.text = @"登录";
    _tips.text = @"没有账号？点此";
    [_switchBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_switchBtn setTitle:@"注册" forState:UIControlStateSelected];
    [_regLoginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_regLoginBtn setTitle:@"登录" forState:UIControlStateSelected];
    _isLogin = true;
    _passwdTextfield.text =  [dic objectForKey:@"password"];
    _userAccountTextField.text = [dic objectForKey:@"username"];
    
    if (kUseJs) {
    } else {
        NSString * userName = [HWKeychain passwordForService:kBundleId account:kUserName];
        NSString * password = [HWKeychain passwordForService:kBundleId account:kPassword];
        userName = userName!=nil ? userName : @"";
        password = password!=nil ? password : @"";
        _passwdTextfield.text =  userName;
        _userAccountTextField.text = userName;
        _passwdTextfield.text =  @"D5NSnR622z1";// [dic objectForKey:@"password"];
        _userAccountTextField.text = @"100056583"; //[dic objectForKey:@"username"];
//        100054142 15361690345 一实名
    }
}

-(void)switchToReg:(NSDictionary*)dic
{
    _title.text = @"注册";
    _tips.text = @"已有账号？点此";
    [_switchBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_switchBtn setTitle:@"登录" forState:UIControlStateSelected];
    
    [_regLoginBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_regLoginBtn setTitle:@"注册" forState:UIControlStateSelected];
    _isLogin = false;
    _passwdTextfield.text = [HWGameMgr randomString:11];
    _userAccountTextField.text = [@"10" stringByAppendingString:[HWGameMgr randomAccount:7]];
}

-(void)login{
    NSDictionary* dic = [[NSDictionary alloc] initWithObjects:@[self.userAccountTextField.text, _passwdTextfield.text ] forKeys:@[@"username", @"password"]];
    if (kUseJs) {
        [[HWGameMgr shared] callToJsWithMethed:kSDKShowLoginCallBack andDic:dic];
    } else {
        [[HWGameMgr shared] httpLogin:dic];
    }
}

-(void)registe{
    NSDictionary* dic = [[NSDictionary alloc] initWithObjects:@[self.userAccountTextField.text, _passwdTextfield.text ] forKeys:@[@"username", @"password"]];
    NSLog(@"账号：%@ 密码：%@",_userAccountTextField.text,_passwdTextfield.text);
    if (kUseJs) {
        [[HWGameMgr shared] callToJsWithMethed:kSDKShowRegisteCallBack andDic:dic];
    } else {
        [[HWGameMgr shared] httpRegister:dic];
    }
}

#pragma mark - 点击事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.passwdTextfield resignFirstResponder];
}

#pragma mark - 限制文字长度
-(void) LimitTextFieldLength:(id)sender{
    if (_userAccountTextField.text.length>kAccountLength) {
        _userAccountTextField.text = [_userAccountTextField.text substringToIndex:kAccountLength];
    }
}
-(void) LimitTextFieldLength1:(id)sender{
    if (_passwdTextfield.text.length>kPasswordLength) {
        _passwdTextfield.text = [_passwdTextfield.text substringToIndex:kPasswordLength];
    }
}

#pragma mark - delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField != _userAccountTextField) {
        return YES;
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

@end
